// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports


namespace SCIEF.SysBFW.CommonExt
{
	namespace Cache
	{
		/// <summary>
		/// Cache implementation by SCIEF
		/// </summary>
		/// <remarks></remarks>
		public class CacheExt
		{
			/// <summary>
			/// Shared because we need the same instance every time we use the cache
			/// </summary>
			/// <remarks></remarks>
			private static System.Runtime.Caching.ObjectCache cache;
			/// <summary>
			/// Shared because we need the same instance every time we use the cache
			/// </summary>
			/// <remarks></remarks>
			private static System.Runtime.Caching.CacheItemPolicy policy;
			
			public CacheExt()
			{
				cache = System.Runtime.Caching.MemoryCache.Default;
				policy = new System.Runtime.Caching.CacheItemPolicy();
				policy.Priority = System.Runtime.Caching.CacheItemPriority.Default;
			}
			
			/// <summary>
			/// Add an object to the cache.
			/// </summary>
			/// <param name="objectName">Name of the object to cache</param>
			/// <param name="cacheObject">The object to cache</param>
			/// <remarks>By design, We store in cache for 60 seconds. (BDesjardins)</remarks>
			public static void AddCacheObject(string objectName, object cacheObject)
			{
				//Testpoint: cache value setting
				policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60.0);
				cache.Set(objectName, cacheObject, policy, null);
			}
			/// <summary>
			/// Add an object to the cache, duration is Infinite
			/// </summary>
			/// <param name="objectName"></param>
			/// <param name="cacheObject"></param>
			/// <remarks>DateTimeOffset.MaxValue is the Infinite value</remarks>
			public static void AddInfiniteCacheObject(string objectName, object cacheObject)
			{
				//Testpoint: cache value setting
				policy.AbsoluteExpiration = DateTimeOffset.MaxValue;
				cache.Set(objectName, cacheObject, policy, null);
			}
			/// <summary>
			/// Find the object in the cache, Return nothing if not found
			/// </summary>
			/// <param name="objectName">Name of the object</param>
			/// <returns>The object in cache</returns>
			/// <remarks>Don't forget to cast your object to the appropriate type</remarks>
			public static object GetCacheObject(string objectName)
			{
				return cache[objectName];
			}
			/// <summary>
			/// Remove each object from the cache
			/// </summary>
			/// <remarks></remarks>
			public static void ClearCache()
			{
				List<KeyValuePair<string, object>> items = (from n in cache.AsParallel() select n).ToList();
				foreach (KeyValuePair<string, object> item in items)
				{
					cache.Remove((string) item.Key, null);
				}
			}
			
		}
	}
	
}
